package com.letv.android.client.fragment;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import com.ledim.bean.ChatRoomBean;
import com.ledim.datastatistics.LeDimAnalysisUtils;
import com.ledim.utils.DensityUtil;
import com.letv.android.client.activity.AlbumPlayRoomActivity;
import com.letv.android.client.album.controller.AlbumRoomGestureController.Type;
import com.letv.android.client.controller.AlbumGestureObservable;
import com.letv.android.client.controller.AlbumGestureObservable.ProgressRegulateNotify;
import com.letv.android.client.controller.ScreenObservable;
import com.letv.android.client.meditor.VideoRoomControllerMeditor;
import com.letv.android.client.view.BaseNetChangeLayer;
import com.letv.android.client.view.LetvSeekBar;
import com.letv.android.client.view.PlayLoadLayout;
import com.letv.android.client.view.ScrollTextView;
import com.letv.android.young.client.R;
import com.letv.business.flow.album.AlbumPlayFlow;
import com.letv.business.flow.album.AlbumPlayFlowObservable;
import com.letv.business.flow.album.AlbumPlayFlowObservable.VideoTitleNotify;
import com.letv.business.flow.album.model.AlbumPlayInfo;
import com.letv.business.flow.play.PlayStatisticsUtils;
import com.letv.core.bean.PlayRecord;
import com.letv.core.constant.LetvConstant.DialogMsgConstantId;
import com.letv.core.constant.PlayConstant;
import com.letv.core.db.PreferencesManager;
import com.letv.core.utils.LogInfo;
import com.letv.core.utils.StatisticsUtils;
import com.letv.core.utils.StringUtils;
import com.letv.core.utils.TipUtils;
import com.letv.core.utils.UIsUtils;
import com.letv.datastatistics.util.DataConstant;
import com.letv.datastatistics.util.PageIdConstant;
import com.letv.tracker2.agnes.VideoPlay;
import com.ledim.utils.ToastUtil;
import com.ledim.ledimview.LedimUserIconView;
import com.ledim.widget.view.PeriscopeGoldLayout;
import java.util.Observable;
import java.util.Observer;

/**
 * 点播videoview控制器fragment基类
 *
 * @author zhuqiao
 */
public abstract class AlbumRoomBaseControllerFragment
        implements VideoRoomControllerMeditor.VideoToControllerListener, Observer, OnClickListener {

    public LinearLayout ll_name_gold;

    public enum Style {
        HALF, FULL, BOTH, NONE
    }

    protected int mLaunchMode; // 启动方式

    // wuxinrong added 标记播放或暂停，
    // 用于全屏、半屏播放控制器中播放、暂停事件的统一处理
    protected boolean mIsPlaying = false;

    /** 等待隐藏的handler msg **/
    protected static final int MSG_WAIT_HIDE = 0x100;
    /** 等待隐藏延迟时间 **/
    private static final int HIDE_DELAY = 3000;

    /** 横竖屏切换时控制器是否显示中 **/
    public static boolean sShowingWhenSwitch = true;

    protected AlbumPlayRoomActivity mActivity;

    protected VideoRoomControllerMeditor mMeditor;

    /** 是否是半屏 **/
    protected boolean mIsHalf;

    /** 拖动进度条之前是否处于播放状态 **/
    protected boolean mIsPlayingBefore;

    /** 是否正在拖动进度 **/
    private boolean mIsProgress;

    /** 是否是4d视频 **/
    protected boolean mIs4d;

    // -------- com.ledim.widget ---------
    /**  **/
    protected View mContentView;
    /** 阻止事件向下传递的view **/
    protected View mInterceptClickView;
    /** 标题 **/
    protected ScrollTextView mTitleTextView;
    /** 播放、暂停按钮 **/
    protected ImageView mPlayImageView;
    /** 切换全半屏按钮 **/
    protected ImageView mSwitchImageView;
    /** 拖动条 **/
    protected LetvSeekBar mSeekBar;

    protected ImageView mVolumeImageView; // 音量按钮

    protected ImageView mScreenOrientationLock; // 屏幕方向锁

    // 切换码流tip(全半屏id是一样的)
    /** 码流提示语layout **/
    protected View mStreamTipView;
    /** 码流提示语文案 **/
    protected TextView mStreamTipTextView;
    /** 码流提示语码流文案 **/
    protected TextView mStreamTipStreamTextView;
    /** 码流取消按钮 **/
    protected Button mStreamCancelView;

    protected View mBackForeverView;// 全屏不显示

    /** 房主信息 **/
    protected PeriscopeGoldLayout mGoldLayout;
    protected FrameLayout mUserView;
    protected TextView mUserName;
    protected TextView mGoldCount;
    protected LinearLayout mGoldView;
    protected LedimUserIconView user_icon_view;

    /** 当前播放信息 **/
    protected LinearLayout mSeekView;
    protected TextView mPlayingName;
    protected TextView mStartTime;
    protected TextView mEndTime;

    /** 是否锁定 **/
    protected boolean mIsLocked;

    /** 是否正在切换码流 **/
    protected boolean mIsSwitch = false;
    /** 用于记录当前切换到的码流 **/
    private String mCurSwitchStream;
    /** 等待切换的码流名称.需要同步到半屏,所以用static **/
    protected static String sWaitingSwitchStreamName = "";

    /** 用户点击返回按钮时间点 **/
    protected String mUserClickBackTime;
    /** 用户点击返回按钮开始时间 **/
    protected long mUserClickBackStartTime;

    /** 网络切换显示的layout **/
    protected BaseNetChangeLayer mNetChangeLayer;

    /**
     * 打上片头和片尾的点
     */
    private int mPlayW = -1;
    private int mSwitchW = -1;
    protected int mHdW = 0;
    protected int mSoundW = 0;

    /**
     * 用于记录最近一次拖动进度条的时间
     */
    protected long mLastProgressTime;

    protected SharedPreferences mShared;

    protected Handler mHandler = new Handler() {

        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_WAIT_HIDE:
                    setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
        }

    };

    public AlbumRoomBaseControllerFragment(Context context, View view) {
        mActivity = (AlbumPlayRoomActivity) context;
        mContentView = view;
    }

    protected void initView() {
        mStreamTipView = mContentView.findViewById(R.id.change_stream_tip_frame);
        mStreamTipTextView = (TextView) mContentView.findViewById(R.id.change_stream_tip_text);
        mStreamTipStreamTextView =
                (TextView) mContentView.findViewById(R.id.change_stream_tip_stream);
        mStreamCancelView = (Button) mContentView.findViewById(R.id.change_stream_tip_cancel);
        mBackForeverView = mActivity.findViewById(R.id.player_half_controller_back_forver);

        mGoldLayout =
                (PeriscopeGoldLayout) mActivity.findViewById(R.id.letv_fragment_player_gold_view);
        mUserView = (FrameLayout) mActivity.findViewById(R.id.letv_fragment_player_user_view);
        mUserName = (TextView) mActivity.findViewById(R.id.letv_fragment_player_user_name);
        mGoldCount = (TextView) mActivity.findViewById(R.id.letv_fragment_player_user_gold);
        mGoldView = (LinearLayout) mActivity.findViewById(R.id.letv_fragment_player_user_gold_view);
        user_icon_view = (LedimUserIconView) mActivity.findViewById(R.id.user_icon_view);
        //用户金币的姓名动画
        ll_name_gold = (LinearLayout) mActivity.findViewById(R.id.ll_name_gold);

        mInterceptClickView.setVisibility(View.GONE);
        mSeekBar.setVisibility(View.GONE);
        mSeekView.setVisibility(View.GONE);
        initLock();

        //        mScreenOrientationLock.setOnClickListener(this);
        mPlayImageView.setOnClickListener(this);

        mInterceptClickView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // do nothing... 拦截掉点击事件
                checkIsPauseAdShowWhenClick();
            }
        });
    }

    /**
     * 初始化房主信息
     *
     * @param
     */
    public void setUserInfo(ChatRoomBean room) {
        if (room != null) {
            if (room.name.length() > 10) {
                mTitleTextView.setText(room.name.substring(0, 10) + "…");
            } else {
                mTitleTextView.setText(room.name);
            }
            if (room.user != null) {
                mUserView.setVisibility(View.VISIBLE);
                mUserName.setText(room.user.nickname);
                mGoldCount.setText(room.user.coin);
                if (room.user.avatar != null && !TextUtils.isEmpty(room.user.avatar.thumb)) {
                    user_icon_view.setUserIcon(room.user.userId, room.user.status, room.user.avatar.thumb, true);
                } else {
                    user_icon_view.setUserIcon(room.user.userId, room.user.status, "", true);
                }
            }
        }

        ViewTreeObserver vto = mUserName.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mUserName.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                if (mGoldView.getWidth() > mUserName.getWidth()) {
                    ViewGroup.LayoutParams params = mUserName.getLayoutParams();
                    params.width = mGoldView.getWidth() + DensityUtil.dip2px(mActivity, 10);
                    mUserName.setLayoutParams(params);
                }
            }
        });

    }

    public void setUserCoin(String coin) {
        mGoldCount.setText(coin);
    }

    public void addUserCoin(int coin) {

        //金币增加动效
        //        mGoldLayout.addHeart(coin, mUserView);

        String countStr = mGoldCount.getText().toString();
        int count = 0;
        if (countStr.length() > 0) {
            count = Integer.valueOf(countStr);
        }
        count = count + coin;
        mGoldCount.setText(count + "");
    }

    /**
     * 初始化lock状态
     */
    private void initLock() {
        if (mActivity.getController() != null) {
            mIsLocked = mActivity.getController().isLock();
            mScreenOrientationLock.setImageResource(
                    mIsLocked ? R.drawable.letv_lock_active : R.drawable.letv_lock_normal);
        }
    }

    public void setMeditor(VideoRoomControllerMeditor meditor) {
        mMeditor = meditor;
    }

    public VideoRoomControllerMeditor getMeditor() {
        return mMeditor;
    }

    /**
     * 更新片头片尾状态
     *
     * @param beginTime
     * @param endTime
     */
    protected void updateSkipState(long beginTime, long endTime) {
        if (mActivity.getFlow() == null) {
            return;
        }
        PlayRecord record = mActivity.getFlow().getPlayRecord();
        AlbumPlayInfo playInfo = mActivity.getFlow().getPlayInfo();

        if (PreferencesManager.getInstance(mActivity).isSkip() && record != null) {
            if (mPlayW == -1) {
                mPlayW = mPlayImageView.getLayoutParams().width;
            }
            //            if (mSwitchW == -1) {
            //                mSwitchW = mSwitchImageView.getLayoutParams().width;
            //            }

            int rootWidth = mIsHalf
                            ? Math.min(UIsUtils.getScreenWidth(mActivity), UIsUtils.getScreenHeight(mActivity))
                            : Math.max(UIsUtils.getScreenWidth(mActivity), UIsUtils.getScreenHeight(mActivity));
            int totalWidth = rootWidth - mPlayW - mSwitchW - mSoundW - mHdW;

            if (beginTime > 0) {
                long duration_time = record.getTotalDuration() > 0 ? record.getTotalDuration()
                                                                   : playInfo.totalTime / 1000;
                int position = (int) (beginTime * totalWidth * 1.0 / duration_time);

                //                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mTitleDot.getLayoutParams();
                //                params.leftMargin = position;
                //                mTitleDot.setLayoutParams(params);
                //                mTitleDot.setVisibility(View.VISIBLE);
            } else {
                //                mTitleDot.setVisibility(View.GONE);
            }

            if (endTime > 0) {
                long duration_time = record.getTotalDuration() > 0 ? record.getTotalDuration()
                                                                   : playInfo.totalTime / 1000;
                int position = (int) (endTime * totalWidth * 1.0 / duration_time);
                //                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mTrailerDot.getLayoutParams();
                //                params.rightMargin = totalWidth - position;
                //                mTrailerDot.setLayoutParams(params);
                //                mTrailerDot.setVisibility(View.VISIBLE);
            } else {
                //                mTrailerDot.setVisibility(View.GONE);
            }
        } else {
            //            mTitleDot.setVisibility(View.GONE);
            //            mTrailerDot.setVisibility(View.GONE);
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        if (isPlayObservableNotice(data)) {
            return;
        } else if (isGestureObservableNotice(data)) {
            return;
        }
        isFlowObservableNotice(data);
    }

    /**
     * 是否是PlayObservable通知
     *
     * @param data
     * @return
     */
    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    private boolean isPlayObservableNotice(Object data) {
        if (data instanceof String) {
            String notify = (String) data;
            if (TextUtils.equals(ScreenObservable.ON_CONFIG_CHANGE, notify)) {
                // 屏幕旋转
                if (isControllerFocus()) {
                    if (sShowingWhenSwitch) {
                        setVisibility(View.VISIBLE);
                        delayHide();
                    } else {
                        //从横屏切换到竖屏
                        setVisibility(View.GONE);
                        if (mActivity.getVipTrailListener() != null) {
                            mActivity.getVipTrailListener().setVisibileWithController(true);
                        }
                    }
                    //横竖屏切换的时候，开启动画
                    // mActivity.cancelUserIconAnim();
                    mActivity.startUserIconAnim();
                } else {
                    setVisibility(View.GONE);
                }

                return true;
            }
        }

        return false;
    }

    /**
     * 是否是GestureObservable通知
     *
     * @param data
     * @return
     */
    private boolean isGestureObservableNotice(Object data) {
        if (data instanceof String) {
            String notify = (String) data;
            if (TextUtils.equals(AlbumGestureObservable.ON_TOUCH_EVENT_UP, notify)) {
                delayHide();
                if (isControllerFocus() && mIsProgress) {
                    mIsProgress = false;
                    // zhuqiao dlna
                    mMeditor.getControllerToVideoListener().onSeekFinish(mSeekBar.getProgress());
                    mMeditor.getControllerToVideoListener().startHandlerTime();
                    if (mActivity.getController() != null) {
                        mActivity.getController().start();
                    }
                    start(false);
                }
                return true;
            }
        } else if (data instanceof ProgressRegulateNotify) {
            ProgressRegulateNotify notify = (ProgressRegulateNotify) data;
            if (isControllerFocus()) {
                if (!mIsProgress &&
                        mMeditor.getControllerToVideoListener().getVideoView() != null) {
                    mIsPlayingBefore =
                            mMeditor.getControllerToVideoListener().getVideoView().isPlaying();
                }

                mStartTime.setText(StringUtils.stringForTimeNoHour(notify.curPos / 1000));
                mSeekBar.setProgress(notify.curPos / 1000);
                mSeekBar.setMax(notify.total / 1000);
                removeHideHandler();
                mIsProgress = true;
                //                mPlayImageView.setImageResource(notify.forward ? R.drawable.letv_kuaijin_normal : R.drawable.letv_kuaitui_normal);
            }
            return true;
        }

        return false;
    }

    /**
     * 是否是PlayAlbumFlowObservable通知
     *
     * @param data
     * @return
     */
    private boolean isFlowObservableNotice(Object data) {
        if (data instanceof VideoTitleNotify) {
            // 设置标题
            String title = ((VideoTitleNotify) data).title;
            if (mActivity.getFlow() != null) {
                if (mActivity.getFlow().isDownloadFile()) {
                    mPlayingName.setText("正在播放：" + title);
                    //                    mTitleTextView.setData(mActivity.getString(R.string.player_local_tip_playerlibs) + title);
                } else if (mLaunchMode == PlayConstant.LAUNCH_MODE_URI && !mIs4d) {
                    mPlayingName.setText("正在播放：" + title);
                    //                    mTitleTextView.setData(mActivity.getString(R.string.player_local_default_tip) + title);
                } else {
                    mPlayingName.setText("正在播放：" + title);
                    //                    mTitleTextView.setData(title);
                }
            }
            return true;
        } else if (data instanceof String) {
            String notify = (String) data;
            if (TextUtils.equals(AlbumPlayFlowObservable.ON_START_FETCHING, notify)) {
                mSeekBar.setBeginTime(0);
                mSeekBar.setVisibility(View.GONE);
                return true;
            } else if (TextUtils.equals(AlbumPlayFlowObservable.ON_CONTROLLER_DISABLE, notify)) {
                disable();
                return true;
            }
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        if (v == mScreenOrientationLock) {
            clickLockBar();
        } else if (v == mPlayImageView) {
            clickPauseOrPlay();
        }
    }

    @Override
    public void setControllerVisibility(int visibility, boolean isForce, Style style) {
        if (style == Style.FULL) {
            if (!mIsHalf) {
                setControllerVisibility(visibility, isForce);
            }
        } else if (style == Style.HALF) {
            if (mIsHalf) {
                setControllerVisibility(visibility, isForce);
            }
        } else if (style == Style.BOTH) {
            setControllerVisibility(visibility, isForce);
        }
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    protected void setControllerVisibility(int visibility, boolean isForce) {
        removeHideHandler();
        if (visibility == View.VISIBLE) {
            setVisibility(View.VISIBLE);
            mActivity.startUserIconAnim();
            delayHide();
        } else {
            if (isForce) {
                setVisibility(View.GONE);
                //mActivity.cancelUserIconAnim();
                mActivity.startUserIconAnim();
            } else {
                delayHide();
            }
        }
    }

    /**
     * 初始化进度
     *
     * @param max
     *         总共
     * @param progress
     *         当前进度
     * @param buffer
     */
    @Override
    public void initProcess(int max, int progress, int buffer) {

        mSeekView.setVisibility(View.VISIBLE);
        mStartTime.setText(StringUtils.stringForTimeNoHour(progress * 1000));
        mEndTime.setText(StringUtils.stringForTimeNoHour(max * 1000));

        // mSeekBar.setVisibility(View.VISIBLE);

        mSeekBar.setEnable(true);
        mSeekBar.setMax(max);
        mSeekBar.setEndTime(max * 1000);
        mSeekBar.setProgress(progress);
        mSeekBar.setSecondaryProgress(buffer);
        mSeekBar.initBeginTextView();
        mSeekBar.setBeginPos(progress);

        if (mActivity.getFlow() != null) {
            updateSkipState(mActivity.getFlow().getPlayInfo().beginTime, mActivity.getFlow()
                                                                                  .getPlayInfo().endTime);
        }

        initSeekBarListener();
        if (mActivity.getGestureController() != null) {
            mActivity.getGestureController().enableGestureUseful();
        }

        if (mActivity.getRecommendController() != null) {
            mActivity.getRecommendController().reset();
        }
    }

    @Override
    public void setTotalTime(int time) {
        mSeekBar.setEnable(true);

        if (TextUtils.equals("" + time, mSeekBar.getTotalTimes())) {
            mEndTime.setText(StringUtils.stringForTimeNoHour(time));
            mSeekBar.setEndTime(time);
            mSeekBar.setBeginTime(0);
            mSeekBar.hideOrShowBeginTime(true);
        }
    }

    @Override
    public void updateProgress(Style style, int progress, int buffer) {
        if (style == Style.FULL) {
            // 来自全屏.同步半屏
            if (mIsHalf) {
                mStartTime.setText(StringUtils.stringForTimeNoHour(progress * 1000));
                mSeekBar.setProgress(progress);
            }
        } else if (style == Style.HALF) {
            // 来自半屏.同步全屏
            if (!mIsHalf) {
                mStartTime.setText(StringUtils.stringForTimeNoHour(progress * 1000));
                mSeekBar.setProgress(progress);
            }
        } else {
            // LogInfo.log("zhuqiao", "buff:" + buffer + ".....percent:" + (buffer / mSeekBar.getMax()));
            if (buffer < 0) {
                mStartTime.setText(StringUtils.stringForTimeNoHour(progress * 1000));
                mSeekBar.setProgress(progress);
            } else {
                mStartTime.setText(StringUtils.stringForTimeNoHour(progress * 1000));
                // zhuqiao 判断dlna
                mSeekBar.setChangeShow(false);
                mSeekBar.setProgress(progress);
                mSeekBar.setSecondaryProgress(buffer);
            }
        }
    }

    @Override
    public void start(boolean showController) {
        mIsPlaying = true;
        mPlayImageView.setImageResource(R.drawable.play_controller_pause);
        mPlayImageView.setEnabled(true);
        mSeekBar.setEnable(true);
        mSeekBar.hideOrShowEndTime(true);
        mInterceptClickView.setVisibility(View.GONE);
        if (showController && isControllerFocus()) {
            setControllerVisibility(View.VISIBLE, true);
        }
    }

    @Override
    public void pause() {
        mIsPlaying = false;
        mPlayImageView.setImageResource(R.drawable.play_controller_play);
        // mPlayImageView.setOnClickListener(mPlayClick);
    }

    @Override
    public void setEnable(boolean enabled) {
        mSeekBar.setEnable(enabled);
        mPlayImageView.setEnabled(enabled);
        if (mVolumeImageView != null) {
            // 只有全屏有
            mVolumeImageView.setEnabled(enabled);
        }
        //zhuqiao setCtrEnabled

    }

    @Override
    public void setBlockBtnVisibile(boolean show) {
    }

    /**
     * 设置显示隐藏进度
     *
     * @param show
     */
    @Override
    public void setSeekbarVisibile(boolean show) {

        //mSeekBar.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    @Override
    public void initSeekBarBeginTextView() {
        // do nothing...
    }

    @Override
    public void onStreamSwitchFinish() {
        mContentView.setBackgroundColor(Color.TRANSPARENT);
        if (mActivity.getGestureController() != null) {
            mActivity.getGestureController().enableGestureUseful();
        }
        switchStreamFinished();
    }

    /**
     * 延迟隐藏
     */
    public void delayHide() {
        if (mActivity.getErrorTopController() != null &&
                mActivity.getErrorTopController().isErrorCodeVisible()) {
            return;
        }

        // 延迟5s隐藏
        removeHideHandler();
        mHandler.sendEmptyMessageDelayed(MSG_WAIT_HIDE, HIDE_DELAY);
    }

    public void removeHideHandler() {
        mHandler.removeMessages(MSG_WAIT_HIDE);
    }

    protected void initSeekBarListener() {
        /**
         * vivo xt3 播放界面不能点击进度条 by wangjun start
         */
        mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            /** 上次滑动进度 **/
            int preProgress = -1;
            long startTime;

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                PlayLoadLayout.mIsShowBlock = false;

                // zhuqiao dlna

                delayHide();
                mMeditor.getControllerToVideoListener().onSeekFinish(seekBar.getProgress());
                if (mActivity.getController() != null) {
                    mActivity.getController().start();
                }
                start(false);
                mActivity.getGestureController().onTouchEventUp();

                if (mActivity.getFlow() != null) {
//                    mActivity.getFlow()
                  PlayStatisticsUtils.updatePlayDataStatistics(mActivity.getApplicationContext(), mActivity.getFlow(), DataConstant.StaticticsVersion2Constatnt.PlayerAction.DRAG, -1, false, startTime, System
                                     .currentTimeMillis());
                    mActivity.getFlow().getPlayInfo().mDragTime =
                            System.currentTimeMillis() - startTime;
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                startTime = System.currentTimeMillis();
                removeHideHandler();
                mActivity.getAlbumPlayFragment().mIsSeekByUser = true;
                if (mMeditor.getControllerToVideoListener().getVideoView() != null) {
                    mIsPlayingBefore =
                            mMeditor.getControllerToVideoListener().getVideoView().isPlaying();
                }
                if (mActivity.getController() != null) {
                    mActivity.getController().pause(false);
                }
                pause();
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                onSeekProgressChanged(seekBar, progress, fromUser);
                if (fromUser) {
                    mMeditor.getControllerToVideoListener().stopHandlerTime();
                    removeHideHandler();
                    // if (countTouchNumber > 3) {
                    mActivity.getGestureController()
                             .progressRegulate(
                                     progress * 1000,
                                     seekBar.getMax() * 1000, true, true, Type.ALBUM);
                    // }

                    if (preProgress != -1) {
                        //                        mPlayImageView.setImageResource(progress > preProgress ? R.drawable.letv_kuaijin_normal
                        //                                : R.drawable.letv_kuaitui_normal);
                    }

                    mLastProgressTime = System.currentTimeMillis();
                }

                preProgress = progress;
                if (mActivity.getFlow() != null) {
                    setEndTime((int) (mActivity.getFlow().getPlayInfo().totalTime / 1000));
                }
                mMeditor.getControllerToVideoListener()
                        .updateProgress(!mIsHalf, mSeekBar.getProgress());
            }
        });
        mSeekBar.setOnSeekBarTouchListener(new LetvSeekBar.OnSeekBarTouchListener() {
            @Override
            public void onSeekBarTouch(MotionEvent event) {
                onSeekBarTouchListener(event);
            }
        });
    }

    protected void onSeekBarTouchListener(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                mMeditor.getControllerToVideoListener().buffTimeSchedule();
                mMeditor.getControllerToVideoListener().startHandlerTime();
                break;
            case MotionEvent.ACTION_DOWN:
                break;
        }
    }

    protected void onSeekProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    }

    /**
     * lockBar点击事件
     */
    private void clickLockBar() {
        mIsLocked = !mIsLocked;
        mScreenOrientationLock.setImageResource(
                mIsLocked ? R.drawable.letv_lock_active : R.drawable.letv_lock_normal);
        if (mIsLocked) {//已锁定
            ToastUtil.toast(TipUtils.getTipMessage(mActivity, DialogMsgConstantId.PLAY_OPERATION_LOCK, R.string.play_operation_lock));
        } else {//已解锁
            ToastUtil.toast(TipUtils.getTipMessage(mActivity, DialogMsgConstantId.PLAY_OPERATION_UNLOCK, R.string.play_operation_unlock));
        }

        if (mActivity.getController() != null) {
            mActivity.getController().setLock(mIsLocked);
        }
        if (isControllerFocus()) {
            delayHide();
        }

        if (UIsUtils.isLandscape(mActivity)) {// 全屏
            if (mIsLocked) {
                StatisticsUtils.staticticsInfoPost(mActivity, "0", "c68", "1014", 1, null, PageIdConstant.fullPlayPage, null, null, null, null, null);
            } else {
                StatisticsUtils.staticticsInfoPost(mActivity, "0", "c68", "1015", 2, null, PageIdConstant.fullPlayPage, null, null, null, null, null);
            }
        } else {// 半屏
            if (mIsLocked) {
                StatisticsUtils.staticticsInfoPost(mActivity, "0", "c70", null, 1, null, PageIdConstant.halpPlayPage, null, null, null, null, null);
            } else {
                StatisticsUtils.staticticsInfoPost(mActivity, "0", "c70", null, 2, null, PageIdConstant.halpPlayPage, null, null, null, null, null);
            }
        }
    }

    /**
     * 播放-暂停按钮的处理
     */
    protected void clickPauseOrPlay() {
        if (mActivity.getController() == null) {
            return;
        }
        if (mIsPlaying) {
            AlbumPlayFlow flow = mActivity.getFlow();
            VideoPlay videoPlay = flow.getCommonVideoPlay();
            videoPlay.pause(mSeekBar.getProgress());
            LeDimAnalysisUtils.reportPlayEvent(videoPlay);
        } else {
            if (mSeekBar.getProgress() != 0) {
                //暂停后重新播放
                AlbumPlayFlow flow = mActivity.getFlow();
                VideoPlay videoPlay = flow.getCommonVideoPlay();
                videoPlay.resume(mSeekBar.getProgress());
                LeDimAnalysisUtils.reportPlayEvent(videoPlay);
            }

        }
    }

    /**
     * 点击的时候暂停广告是否正在显示
     *
     * @return
     */
    protected boolean checkIsPauseAdShowWhenClick() {
        if (UIsUtils.isLandscape(mActivity) && mActivity.getFlow() != null &&
                mActivity.getPlayAdListener() != null) {
            if (mActivity.getFlow().isPauseAdIsShow()) {
                mActivity.getPlayAdListener().closePauseAd();
                mInterceptClickView.setVisibility(View.GONE);
                return true;
            }
        }

        return false;
    }

    /**
     * 显示正在切换码流的提示
     */
    public void showStreamTip(String streamName) {
        mContentView.setBackgroundColor(Color.BLACK);
        mIsSwitch = true;
        mCurSwitchStream = streamName;
        mStreamTipView.setVisibility(View.VISIBLE);
        mStreamTipStreamTextView.setVisibility(View.GONE);
        mStreamCancelView.setVisibility(View.GONE);

        mStreamTipTextView.setText(TipUtils.getTipMessage(mActivity, DialogMsgConstantId.SWITCH_STREAM, R.string.switch_hd_start));
    }

    /**
     * 切换码流完成
     */
    private void switchStreamFinished() {
        if (mIsSwitch) {
            mStreamTipView.setVisibility(View.VISIBLE);
            mStreamTipTextView.setText(R.string.switch_hd_end);
            mStreamTipStreamTextView.setText(mCurSwitchStream);
            mStreamTipTextView.setVisibility(View.VISIBLE);
            mStreamTipStreamTextView.setVisibility(View.VISIBLE);
            mStreamCancelView.setVisibility(View.GONE);
            new Handler() {
                public void handleMessage(Message msg) {
                    mStreamTipView.setVisibility(View.GONE);
                    delayHide();
                }

            }.sendEmptyMessageDelayed(1, 1500);
            mIsSwitch = false;
        }
    }

    public void setEndTime(int max) {
        if (mIsHalf) {
            mSeekBar.setEnable(true);
            mEndTime.setText(StringUtils.stringForTimeNoHour(max * 1000));
            mSeekBar.setEndTime((max * 1000));
        }
        mSeekBar.setMax(max);
    }

    /**
     * 由于当前基类控制了全半屏,所以需要判断当前是否处于可控状态
     *
     * @return 全屏时，判断controller是否为全屏controller
     */
    protected boolean isControllerFocus() {
        if (UIsUtils.isLandscape(mActivity)) {
            return !mIsHalf;
        }

        return mIsHalf;
    }

    /**
     * 禁用
     */
    private void disable() {
        mPlayImageView.setImageResource(R.drawable.play_controller_play);
        mStartTime.setText(StringUtils.stringForTimeNoHour(0));
        mEndTime.setText(StringUtils.stringForTimeNoHour(0));
        mSeekBar.setProgress(0);
        mSeekBar.setEndTime(0);
        mSeekBar.setMax(0);
        mSeekBar.setSecondaryProgress(0);
        mSeekBar.setEnable(false);
        //        mTitleDot.setVisibility(View.GONE);
        //        mTrailerDot.setVisibility(View.GONE);
        // mInterceptClickView.setVisibility(View.VISIBLE);
        removeHideHandler();
    }

    public void setInterceptClick() {
        mInterceptClickView.setVisibility(View.VISIBLE);
    }

    /**
     * 设置是否显示推荐提示
     */
    private void checkShowVideoRecommend(boolean show) {
        if (mActivity == null || mActivity.getRecommendController() == null ||
                mActivity.getFlow() == null) {
            return;
        }

        if (!show) {
            mActivity.getRecommendController().hideRecommendTipView();
            return;
        }

        AlbumPlayInfo playInfo = mActivity.getFlow().getPlayInfo();
        LogInfo.log("zhuqiao", "checkShowVideoRecommend");
        if (mActivity.getFlow().isSkip() && playInfo.endTime > 0) {
            mActivity.getRecommendController()
                     .showPlayRecommendTip(playInfo.currTime, playInfo.endTime);
        } else {
            mActivity.getRecommendController()
                     .showPlayRecommendTip(playInfo.currTime, playInfo.totalTime / 1000);
        }
    }

    public void setVisibility(int visibility) {
        mContentView.setVisibility(visibility);
        boolean hidden = visibility != View.VISIBLE;
        mUserClickBackStartTime = 0;
        mUserClickBackTime = "";
        initLock();
        if (mActivity.getVipTrailListener() != null && isControllerFocus()) {
            mActivity.getVipTrailListener().setIsControllerVisible(!hidden);
            mActivity.getVipTrailListener().setVisibileWithController(hidden);
        }
        if (!hidden) {
            mHandler.postDelayed(new Runnable() {

                @Override
                public void run() {
                    // 防止闪烁
                    mBackForeverView.setVisibility(View.GONE);
                }
            }, 200);
        }
        if (isControllerFocus()) {
            checkShowVideoRecommend(hidden);
            sShowingWhenSwitch = !hidden;
        }
    }

    public boolean isVisible() {
        return mContentView.getVisibility() == View.VISIBLE;
    }

    public void onDestroy() {
        mTitleTextView.cancel();
    }

    // ----------------- get set -----------------
    public String getUserClickBackTime() {
        return mUserClickBackTime;
    }

    public long getUserClickBackStartTime() {
        return mUserClickBackStartTime;
    }
}
